<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 9:11
 */
class ZduScreenAction extends BaseAction
{
    //pad端
    public function padIndex(){
        $type = $this->_request('type');
        $today =  strtotime(date("Y-m-d"));
        $mOrder = M("restaurant_order_goods");
        $restaurantId = $this->_request("restaurant_id");
        $view = $this->_request('view');
        //搜索条件
        $good_tj = $this->_request("good_tj");
        //type = 1 新菜单; 2 未取菜  默认为新菜单
        $type = $type ? $type : 3;
        if($type !=3){
            $where_type='';
            $where='';
            if($good_tj){
                $where = "AND (goods_name like '%{$good_tj}%' or order_num = '{$good_tj}')";
            }
            if($type ==1){
                $type = 0;
                $where_type = "AND goods_status = {$type}";
            }
            if($type ==2){
                $type = 1;
                $where_type = "AND goods_status = {$type}";
            }
            if($restaurantId !='') {
				/*$goods = $mOrder->field('og_id,goods_id,goods_num,goods_name,order_num,goods_status,create_time')->where("restaurant_id = {$restaurantId} AND pay_status =1 AND window_id <> 52 AND category_id <> 149 AND create_time >= {$today} {$where} {$where_type}")->order('create_time ASC')->select();*/
                $goods = $mOrder->field('og_id,goods_id,goods_num,goods_name,order_num,goods_status,create_time')->where("pay_status =1 AND window_id <> 52 AND category_id <> 125 AND create_time >= {$today} {$where} {$where_type}")->order('create_time ASC')->select();
                vendor('Py.PinYin');
                $py = new PinYin();
                foreach ($goods as $k=>$v){
                    $goods[$k]['alert'] = 0;
                    if((time()-$v['create_time'])>1800&&$v['goods_status'] ==0){
                        $goods[$k]['alert'] = 1;
                    }
                    $goods[$k]['first'] = substr($py->getFirstPY($v['goods_name']),0,1);
                }
                $goods = $this->sigcol_arrsort($goods,'first',SORT_ASC);
                $goodsList = array();
                foreach ($goods as $key=>$v){
                    $goodsList[$v['goods_name']][] = $v;
                }
                $this->assign('goods', $goods);
                $this->assign('goodsList', $goodsList);
                $this->assign('good_tj', $good_tj);
            }
        }
        if($type ==3){
            $restaurantId = $this->_request("restaurant_id");
            //搜索条件
            $condition = array();
            $condition['pay_status'] = 1;
            //$condition['restaurant_id'] = $restaurantId;
            $condition['create_time'] =array('gt',$today);
            if($good_tj){
                $condition['order_num'] = $good_tj;
            }
            $card = M('CampuscardCard');
            $res = $mOrder->field('og_id,goods_id,goods_num,goods_name,order_num,goods_status,user_id,create_time')->where($condition)->order('create_time ASC')->select();
            $goodsList = array();
            foreach ($res as $a=>$arr){
                $res[$a]['alert'] = 0;
                $res[$a]['number'] = $card->where(array('user_id'=>$arr['user_id']))->getField('number2');
                if((time()-$arr['create_time'])>1800&&$arr['goods_status'] == 0){
                    $res[$a]['alert'] = 1;
                }
            }
            foreach ($res as $key=>$v){
                $goodsList[$v['order_num']][] = $v;
            }
        }

        if($this->isAjax()){
            $this->ajaxReturn(array('status'=>'S','message'=>'请求成功','data'=>$goodsList,'type'=>$type));
        }
        $this->assign('type', $type);
        $this->assign('restaurant_id', $restaurantId);
        if($view == 'push'){
            $this->display();
        }else{
            $this->display('pullindex');
        }
    }

    public function pullIndex(){
        $type = $this->_request('type');
        $today =  strtotime(date("Y-m-d"));
        $mOrder = M("restaurant_order_goods");
        $restaurantId = $this->_request("restaurant_id");
        //搜索条件
        $good_tj = $this->_request("good_tj");
        //type = 1 新菜单; 2 未取菜  默认为新菜单
        $type = $type ? $type : 3;
        if($type !=3){
            $where_type='';
            $where='';
            if($good_tj){
                $where = "AND (goods_name like '%{$good_tj}%' or order_num = '{$good_tj}')";
            }
            if($type ==1){
                $type = 0;
                $where_type = "AND goods_status = {$type}";
            }
            if($type ==2){
                $type = 1;
                $where_type = "AND goods_status = {$type}";
            }
            if($restaurantId !='') {
                $goods = $mOrder->field('og_id,goods_id,goods_num,goods_name,order_num,goods_status')->where("restaurant_id = {$restaurantId} AND pay_status =1 AND window_id <> 52 AND category_id <> 149 AND create_time >= {$today} {$where} {$where_type}")->order('create_time ASC')->select();
                vendor('Py.PinYin');
                $py = new PinYin();
                $goodsList = array();
                foreach ($goods as $k=>$v){
                    $goods[$k]['first'] = substr($py->getFirstPY($v['goods_name']),0,1);
                }
                $goods = $this->sigcol_arrsort($goods,'first',SORT_ASC);
                $goodsList = array();
                foreach ($goods as $key=>$v){
                    $goodsList[$v['goods_name']][] = $v;
                }
                //cprint(json_encode($goodsList));die;
                $this->assign('goods', $goods);
                $this->assign('goodsList', $goodsList);
                $this->assign('good_tj', $good_tj);
            }
        }
        if($type ==3){
            $restaurantId = $this->_request("restaurant_id");
            //搜索条件
            $condition = array();
            $condition['pay_status'] = 1;
            $condition['restaurant_id'] = $restaurantId;
            $condition['create_time'] =array('gt',$today);
            if($good_tj){
                $condition['order_num'] = $good_tj;
            }
            $card = M('CampuscardCard');
            $res = $mOrder->field('og_id,goods_id,goods_num,goods_name,order_num,goods_status,user_id')->where($condition)->order('create_time ASC')->select();
            $goodsList = array();
            foreach ($res as $a=>$arr){
                $res[$a]['number'] = $card->where(array('user_id'=>$arr['user_id']))->getField('number2');
            }
            foreach ($res as $key=>$v){
                $goodsList[$v['order_num']][] = $v;
            }
            //cprint($goodsList);die;
        }

        if($this->isAjax()){
            $this->ajaxReturn(array('status'=>'S','message'=>'请求成功','data'=>$goodsList,'type'=>$type));
        }
        $this->assign('type', $type);
        $this->assign('restaurant_id', $restaurantId);
        $this->display();
    }

    private function sigcol_arrsort($data,$col,$type=SORT_DESC){
        if(is_array($data)){
            $i=0;
            foreach($data as $k=>$v){
                if(key_exists($col,$v)){
                    $arr[$i] = $v[$col];
                    $i++;
                }else{
                    continue;
                }
            }
        }else{
            return false;
        }
        array_multisort($arr,$type,$data);
        return $data;
    }

    //大屏幕
    public function screenIndex(){
        $today =  strtotime(date("Y-m-d"));
        $M_restaurant = M("restaurant_info");
        $restaurant_id = $this->_get("restaurant_id");
        $restaurant_data = $M_restaurant->where("restaurant_id={$restaurant_id}")->find();

        $page= 8;
        $M_log=M('restaurant_order_goods_log');
        $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND order_num !=0 AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->select();
        $datas = json_encode(array_chunk($log_data,$page));

        $this->assign('restaurant_name',$restaurant_data['restaurant_name']);
        $this->assign('log_data',$log_data);
        $this->assign('datas',$datas);

        $this->assign('restaurant_id', $restaurant_id);

        $this->display();

    }

    public function oneScreen(){
        $today =  strtotime(date("Y-m-d"));
        $M_restaurant = M("restaurant_info");
        $restaurant_id = $this->_get("restaurant_id");
        $restaurant_data = $M_restaurant->where("restaurant_id={$restaurant_id}")->find();
        $p = $this->_request('p') ? $this->_request('p') :1;
        $num = 3;
        $M_log=M('restaurant_order_goods_log');
        $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->select();
        $count = count($log_data);
        if($p>ceil($count/$num)){
            $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->limit(0,3)->select();
            $this->assign('p',$p);
            $this->assign('datas',$log_data);
            $this->assign('restaurant_id', $restaurant_id);
            $this->assign('count',count($log_data));
            $this->display();return;
        }
        $page = new Page($count, $num);
        /*        $page->totalPages   =   ceil($page->totalRows/$page->listRows);     //总页数
                if($p>$page->totalPages){

                }*/
        $limit = $page->firstRow . ',' . $page->listRows;
        $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->limit($limit)->select();
        $this->assign('restaurant_name',$restaurant_data['restaurant_name']);
        //cprint($log_data);die;
        $this->assign('restaurant_id', $restaurant_id);
        $this->assign('p',$p+2);
        $this->assign('datas',$log_data);
        $this->assign('count',count($log_data));
        $this->display();
    }

    public function twoScreen(){
        $today =  strtotime(date("Y-m-d"));
        $M_restaurant = M("restaurant_info");
        $restaurant_id = $this->_get("restaurant_id");
        $restaurant_data = $M_restaurant->where("restaurant_id={$restaurant_id}")->find();
        $p = $this->_request('p') ? $this->_request('p') :2;
        $num = 3;
        $M_log=M('restaurant_order_goods_log');
        $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->select();
        $count = count($log_data);
        if($p>ceil($count/$num)){
            $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->limit(3,3)->select();
            $this->assign('datas',$log_data);
            $this->assign('restaurant_id', $restaurant_id);
            $this->assign('count',count($log_data));
            $this->display();return;
        }
        $page = new Page($count, $num);
        $limit = $page->firstRow . ',' . $page->listRows;
        $log_data = $M_log->where("goods_status = 1 AND creation_time >= {$today} AND restaurant_id={$restaurant_id}")->order('creation_time ASC')->limit($limit)->select();
        $this->assign('restaurant_name',$restaurant_data['restaurant_name']);
        //cprint($log_data);die;
        $this->assign('restaurant_id', $restaurant_id);
        $this->assign('p',$p+2);
        $this->assign('datas',$log_data);
        $this->assign('count',count($log_data));
        $this->display();
    }


    //修改订单菜单状态
    public function doType(){
        $ogId = $this->_post("ogid");
        $ogIds = explode(',',$ogId);
        array_pop($ogIds);
        $type = $this->_post("type")+1;
        $restaurantId = $this->_post('restaurant_id');
        $res = 0;
        if(!empty($ogIds) && ($type==1 || $type ==2)) {
            $M_order = M("restaurant_order_goods");
            $condition['og_id'] = array('in',$ogIds);
            $condition['restaurant_id'] = $restaurantId;
            $res = $M_order->where($condition)->save(array('goods_status' => $type));

            if($res) {
                //增加大屏幕上菜记录
                $M_log = M("restaurant_order_goods_log");
                if ($type == 1) {
                    foreach ($ogIds as $id){
                        $og_data = $M_order->where("og_id={$id}")->find();
                        $data['order_id'] = $og_data['order_id'];
                        $data['restaurant_id'] = $og_data['restaurant_id'];
                        $data['goods_name'] = $og_data['goods_name'];
                        $data['og_id'] = $id;
                        $data['order_num'] = $og_data['order_num'];
                        $data['goods_logo'] = $og_data['goods_logo'];
                        $data['up_time'] = time();
                        $data['creation_time'] = time();
                        $data['goods_status'] = 1;
                        $M_log->data($data)->add();
                    }
                } elseif ($type == 2) {
                    $res = $M_log->where($condition)->save(array('goods_status' => 2, 'up_time' => time()));
                }
            }
        }
        if($res){
            $this->ajaxReturn(array('status'=>'S','message'=>'操作成功'));
        }else{
            $this->ajaxReturn(array('status'=>'F','message'=>'操作失败'));
        }
        exit;
    }
}